<html>
	<head>
		<title>Logged in</title>
		<script type="text/javascript">
		function diffAdmin(x,mark)
		{
			var append="";
			var url = 'http://vark.myrpi.org/changeAdmin.php?path=' + x + '&flag=' + mark;
		
			if (window.XMLHttpRequest) {
				ajax = new XMLHttpRequest();
			} 

			ajax.onreadystatechange=function()	{
				ajax.timeout = '1';
	
					if(ajax.readyState == 4)	{
						append = "<input id='"+x+"' type='checkbox' onclick='diffAdmin(this.id," + ajax.responseText + ")' ";
						
						if(ajax.responseText == 1)
							append += "' checked='checked'";
						append += " />";
						document.getElementById("td_" + x).innerHTML = append;
						append = "";
					}
			
			}
		
			ajax.open("GET",url);
			ajax.send();
		
		}

		</script>

	</head>
	<body>

<?php

	$order = array('username','admin');

	$conn =  mysql_connect("localhost", "vandes4_root", 'root');
	
	if ($conn)	{
	
	 	$db=mysql_select_db('vandes4_PROJECT', $conn);
	 	
	 	if ($db)	{
	 	
	 		$query = "SELECT username,admin FROM users;";
	 		$exec = mysql_query($query);
			
			echo "<table border=1>";
			
			echo "<th>User</th>";
			echo "<th>Admin</th>";
//			if($admin)	echo "<th>Admin?</th>";
			
			while($read = mysql_fetch_array($exec))	{
				
				$_SESSION['atad'] = $read['admin'];
				$name = $read['username'];
				$end = 0;
				if($admin) $end = 1;
				else $end = 2;
				echo "<tr>";
				
				for ($i = 0; $i < $end; $i++)	{	
						echo "<td class='".$name."'>".$read[$i]."</td>";				
				}
				
				if($admin)	{
				
					echo "<td id='td_".$name."'>"."<input id='".$name."' type='checkbox' onclick='diffAdmin(this.id,".$_SESSION['atad'].")' ";
					$read['admin'] = $_SESSION['atad'];
					if($read['admin'] == 1)
						echo "' checked='checked'";
//					if($name == $_SERVER['PHP_AUTH_USER'])
//						echo " disabled";
					echo " /></td>";
				
				}
				
				echo "</tr>";
			
			}		
			
			echo "</table>";

			
		}
	}

?>
	</body>
</html>